﻿using Dyao.Book.Core;
using Foundation.Core;
using Foundation.Core.ObjectMapping;
using Foundation.Service.Bus.Core;
using Foundation.Uow;
using Microsoft.Extensions.DependencyInjection;

namespace Dyao.Book.Application
{
    public class BaseApplication : ITransientDependency
    {
        private IJsonSerializer _jsonSerializer;

        private IUnitOfWorkManager _unitOfWorkManager;

        private IObjectMapper _objectMapper;

        private ICurrentUser _currentUser;

        private IServiceBus _serviceBus;

        [Injected]
        public IServiceProvider ServiceProvider { get; set; }

        protected IJsonSerializer JsonSerializer => ServiceProvider.GetRequiredServiceReference(ref _jsonSerializer);

        protected IUnitOfWorkManager UnitOfWorkManager => ServiceProvider.GetRequiredServiceReference(ref _unitOfWorkManager);

        protected IObjectMapper ObjectMapper => ServiceProvider.GetRequiredServiceReference(ref _objectMapper);

        protected ICurrentUser CurrentUser => ServiceProvider.GetRequiredServiceReference(ref _currentUser);

        protected IServiceBus ServiceBus => ServiceProvider.GetRequiredServiceReference(ref _serviceBus);

        protected T GetService<T>()
        {
            return ServiceProvider.GetService<T>();
        }
    }
}
